/**
 * @param {Object} data
 */
const standard = function(data) {
	var value = parseInt(data);
	if (isNaN(value) || value <= 0) {
		return value;
	}
	return value < 10 ? ('0' + value) : value;
}

/**
 * 格式化时间
 * @param {Object} date
 */
const formatterDate = function(date) {
	var data = [];
	data.push(date.getFullYear());
	data.push(standard(date.getMonth() + 1));
	data.push(standard(date.getDate()));
	return data.join('-');
}

const currentDate = function() {
	return new Date();
}

const millisecond = 86400000;


class DateFormat {

	constructor(data) {
		this.data = data;
		this.instance = this.init(data);
		this.year = this.instance.getFullYear();
		this.month = this.concat(this.instance.getMonth() + 1);
		this.day = this.concat(this.instance.getDate());
		this.hour = this.concat(this.instance.getHours());
		this.min = this.concat(this.instance.getMinutes());
		this.sec = this.concat(this.instance.getSeconds());

		this.dateText = this.year + '-' + this.month + '-' + this.day;
		this.timeText = this.hour + ':' + this.min + ':' + this.sec;
		this.datetimeText = this.dateText + ' ' + this.timeText;
		this.normal = this.dateText + ' ' + this.hour + ':' + this.min;
		this.unix = parseInt(this.instance.getTime() / 1000);
	}
	/**
	 * @param {Object} value
	 */
	concat(value) {
		value = parseInt(value);
		if (value < 10) {
			value = '0' + value;
		}
		return value;
	}

	init(data) {

		var date = new Date();

		if (typeof data === 'string' || typeof data === "number") {
			data += '';
			if (/^[0-9]+$/.test(data)) {
				if (data.length < 11) {
					data += '000';
				}
				date = new Date(parseInt(data));
			} else {
				let array = data.split(/[- : \/]/);

				if (array.length >= 3) {
					date = new Date(array[0], array[1] - 1, array[2], array[3] || '00', array[4] || '00', array[
						5] || '00');
				}
			}
		} else if (data !== null && typeof data === "object") {
			date = data;
		}

		return date;
	}

	pastTimes() {

		//拆分传来的日期，获取详细数值
		var year = 0; //年份
		var month = 0; //月份
		var day = 0; //日
		var hour = 0; //小时
		var minute = 0; //分
		var second = 0; //秒

		var datetime = this.datetimeText.split(" ");

		var str1 = datetime[0].split("-");
		for (var j = 0; j < str1.length; j++) {
			year = parseInt(str1[0]);
			month = parseInt(str1[1]);
			day = parseInt(str1[2]);
		}

		var str2 = datetime[1].split(":");
		for (var j = 0; j < str2.length; j++) {
			hour = parseInt(str2[0]);
			minute = parseInt(str2[1]);
			second = parseInt(str2[2]);
		}


		var cd = new Date();
		var years = cd.getFullYear(); //获取年份
		var months = cd.getMonth() + 1; //获取月份 ，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
		var days = cd.getDate(); //获取日
		var hours = cd.getHours(); //小时  //Calendar.HOUR是12小时制
		var minutes = cd.getMinutes(); //分

		var getMin = function(min) {
			if (min < 10) {
				min = '0' + min;
			}

			return min;
		};

		//比较是不是今天
		if (year === years && month === months && day === days) {
			//如果是今天，验证是不是同一个小时
			if (hour === hours) {
				//验证是不是同一分钟
				if (minute === minutes || (minutes - minute) === 1) {
					//是同一分钟，返回刚刚
					return "刚刚";
				} else {
					return (minutes - minute) + "分钟前";
				}
			} else {

				var time = '';

				//验证时区
				if (hour <= 6) { //7点之前
					time = "凌晨";
				}

				if (hour > 6 && hour <= 12) { //9-12点
					time = "上午";
				}

				if (hour > 12 && hour <= 17) { //14-18点
					time = "下午";
				}
				if (hour > 17) { //20以后
					time = "晚上";
				}

				return time + ' ' + hour + ':' + getMin(minute);
			}
			//比较是不是昨天
		} else if (year === years && month === months && (days - day) === 1) {
			return "昨天 " + hour + ':' + getMin(minute);
			//比较是不是今年
		} else if (year === years) {
			return month + "月" + day + "日";
		} else {
			return year + "-" + month + "-" + day + ' ' + hour + ":" + minute;
		}
	}

	today(time) {
		var startStop = new Array();
		let begin = new Date(new Date(new Date().toLocaleDateString()).getTime());
		let end = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);

		begin = formatterDate(begin);
		end = formatterDate(end);
		if (time === true) {
			begin += ' 00:00:00';
			end += ' 23:59:59';
		}
		return [
			begin,
			end,
		];
	}

	/**
	 * 获取本周的起止时间
	 */
	getThisWeek() {
		//起止日期数组
		var startStop = new Array();
		var now = currentDate();
		var ms = millisecond;
		//返回date是一周中的某一天
		var week = now.getDay();
		//减去的天数
		var minusDay = week != 0 ? week - 1 : 6;
		//本周 周一
		var monday = new Date(now.getTime() - (minusDay * ms));
		//本周 周日
		var sunday = new Date(monday.getTime() + (6 * ms));
		//添加本周时间
		startStop.push(formatterDate(monday)); //本周起始时间
		//添加本周最后一天时间
		startStop.push(formatterDate(sunday)); //本周终止时间
		//返回
		return startStop;
	}

	/**
	 * 获取上周的起止时间
	 */
	getLastWeek() {
		//起止日期数组
		var startStop = new Array();
		var now = currentDate();
		var ms = millisecond;

		//返回date是一周中的某一天
		var week = now.getDay();
		//减去的天数
		var minusDay = week != 0 ? week - 1 : 6;
		//获得当前周的第一天
		var currentWeekDayOne = new Date(now.getTime() - (ms * minusDay));
		//上周最后一天即本周开始的前一天
		var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - ms);
		//上周的第一天
		var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (ms * 6));

		startStop.push(formatterDate(priorWeekFirstDay));
		startStop.push(formatterDate(priorWeekLastDay));
		//返回
		return startStop;
	}

	/**
	 * 获得这个月的起止日期
	 */
	getThisMonth() {
		var startStop = new Array();

		var now = currentDate();
		var ms = millisecond;

		//获得当前月份0-11
		var currentMonth = now.getMonth();
		//获得当前年份4位年
		var currentYear = now.getFullYear();
		//求出本月第一天
		var firstDay = new Date(currentYear, currentMonth, 1);
		//当为12月的时候年份需要加1
		//月份需要更新为0 也就是下一年的第一个月
		//否则只是月份增加,以便求的下一月的第一天
		if (currentMonth == 11) {
			currentYear++;
			currentMonth = 0;
		} else {
			currentMonth++;
		}
		//下月的第一天
		var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
		//求出上月的最后一天
		var lastDay = new Date(nextMonthDayOne.getTime() - ms);

		startStop.push(formatterDate(firstDay));
		startStop.push(formatterDate(lastDay));
		//返回
		return startStop;
	}

	/**
	 * 获得上个月的起止日期
	 */
	getLastMonth() {
		var startStop = new Array();

		var now = currentDate();
		var ms = millisecond;

		//获得当前月份0-11
		var currentMonth = now.getMonth();
		//获得当前年份4位年
		var currentYear = now.getFullYear();
		var currentDay = new Date(currentYear, currentMonth, 1);
		//上个月的第一天
		//年份为0代表,是本年的第一月,所以不能减
		if (currentMonth == 0) {
			currentMonth = 11; //月份为上年的最后月份
			currentYear--; //年份减1
		} else {
			currentMonth--;
		}
		var firstDay = new Date(currentYear, currentMonth, 1);
		//求出上月的最后一天
		var lastDay = new Date(currentDay.getTime() - ms);

		startStop.push(formatterDate(firstDay));
		startStop.push(formatterDate(lastDay));
		//返回
		return startStop;
	}
}

export default DateFormat;
